package _022_2_12

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

type result []*splitNode

type splitNodeMgr struct {
	root splitNode
}

func (this *splitNodeMgr) add(splitNode, nodeL *splitNode, nodeR *splitNode) {
	splitNode.left = nodeL
	splitNode.right = nodeR
}

func (this result) Len() int {
	return len(this)
}
func (this result) Less(i, j int) bool {

	if this[i].temp < this[j].temp {
		return true
	}

	return false
}
func (this result) Swap(i, j int) {
	temp := this[i]

	this[i] = this[j]

	this[j] = temp
}

func (this *splitNodeMgr) searchInsertNode(seqI int) *splitNode {
	node := &this.root

	if node.left == nil && node.right == nil {
		return node
	}

	if seqI <= this.root.left.end {
		node = this.root.left
	} else {
		node = this.root.right
	}

	for {
		if node.left == nil && node.right == nil {
			return node
		}

		if node.left != nil && seqI <= node.left.end {

			node = node.left
		} else {

			node = node.right
		}
	}
}

type splitNode struct {
	beg   int
	end   int
	temp  int
	left  *splitNode
	right *splitNode
}

func printMaxHeap(m result, maxSplitNode *splitNode) {
	sort.Sort(m)
	fmt.Println(m[len(m)-1].temp)
	*maxSplitNode = *m[len(m)-1]
}

func getMaxHeapGood2(w []int, seq []int) {
	r := result{}
	var maxSplitNode splitNode
	mgr := splitNodeMgr{}

	splitGood := make([][]int, 0)
	splitGood = append(splitGood, []int{0, len(seq) - 1})
	for i := 0; i < len(seq); i++ {

		goodI := seq[i]

		node := mgr.searchInsertNode(goodI)
		if node == &mgr.root {
			node1 := &splitNode{}
			node1.beg = node.beg
			node1.end = goodI - 1

			node2 := &splitNode{}
			node2.beg = goodI + 1
			node2.end = len(seq) - 1

			node.left = node1
			node.right = node2

			temp := 0
			for i1 := node1.beg; i1 <= node1.end; i1++ {
				temp += w[i1]
			}

			node1.temp = temp
			r = append(r, node1)
			temp = 0
			for i1 := node2.beg; i1 <= node2.end; i1++ {
				temp += w[i1]
			}
			node2.temp = temp
			r = append(r, node2)

			printMaxHeap(r, &maxSplitNode)
			continue
		}

		var node1 *splitNode
		var node2 *splitNode
		if node.beg <= goodI-1 {
			node1 = &splitNode{}
			node1.beg = node.beg
			node1.end = goodI - 1
			temp := 0
			for i1 := node1.beg; i1 <= node1.end; i1++ {
				temp += w[i1]
			}
			node1.temp = temp
			r = append(r, node1)
		}

		if node.end >= goodI+1 {
			node2 = &splitNode{}
			node2.beg = goodI + 1
			node2.end = node.end

			temp := 0
			for i1 := node2.beg; i1 <= node2.end; i1++ {
				temp += w[i1]
			}
			node2.temp = temp
			r = append(r, node2)
		}
		mgr.add(node, node1, node2)
		printMaxHeap(r, &maxSplitNode)
	}

}

func main() {
	stdinR := bufio.NewReader(os.Stdin)
	num := 0
	fmt.Fscan(stdinR, &num)

	w := make([]int, num)

	for i := 0; i < num; i++ {
		fmt.Fscan(stdinR, &w[i])
	}

	fetchSeq := make([]int, num)

	for i := 0; i < num; i++ {
		fmt.Fscan(stdinR, &fetchSeq[i])
		fetchSeq[i] -= 1
	}

	getMaxHeapGood(w, fetchSeq)
}
